using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Oops.Xna.Framework.Input;
using Oops.Xna.Framework.Physics;

namespace Oops.Xna.Framework.Testing.App
{
	public class Example5Screen : ExampleScreen
	{
		// Components.
		private CursorComponent cursor;

		// Content.
		private Texture2D planeTexture;

		// Drawing helpers.
		private Quadrilateral quad;

		private CollisionPlane plane;
		private CollisionGroup selectedCollisionGroup;

		public Example5Screen(Game game)
			: base(game, "Example5")
		{
			// Create the cursor component.
			this.cursor = new CursorComponent(this.Game);

			// Add the component to the game-screen's component collection.
			this.Components.Add(this.cursor);

			// Drawing helper for the ground.
			this.quad = new Quadrilateral(Vector3.Zero, Vector3.Up, Vector3.Backward, 260.0f, 260.0f, 10, 10);
		}

		#region ExampleScreen Members

		protected override void DrawShape(Effect effect, CollisionShape collisionShape)
		{
			if (this.selectedCollisionGroup == collisionShape.CollisionGroup)
			{
				effect.Parameters["DiffuseColor"].SetValue(Color.Red.ToVector3());
			}
			else
			{
				effect.Parameters["DiffuseColor"].SetValue(Color.White.ToVector3());
			}

			// Call inherited method.
			base.DrawShape(effect, collisionShape);
		}

		protected override void DrawWorld(Effect effect, EffectParameter world, EffectParameter texture)
		{
			// Draw the ground.
			world.SetValue(Matrix.Identity);
			texture.SetValue(this.planeTexture);
			this.DrawShape(effect, this.plane);
			effect.CommitChanges();
			this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, this.quad.Vertices, 0, 4, this.quad.Indices, 0, 2);
		}

		protected override void Reset()
		{
			// Start with 50 boxes and 150 spheres.
			this.Reset(50, 150);

			// Create the ground plane.  
			this.plane = new CollisionPlane();
			this.plane.Normal = Vector3.UnitY;

			// Create a collision group and add the plane.
			CollisionGroup collisionGroup = new CollisionGroup();
			collisionGroup.CollisionShapes.Add(this.plane);

			// Add the plane's collision group to the collision system.
			this.physics.CollisionSystem.CollisionGroups.Add(collisionGroup);
		}

		#endregion

		#region DrawableGameComponent Members

		protected override void LoadContent()
		{
			// Load all the used texture resources.
			this.planeTexture = this.Game.GetContent().Load<Texture2D>(@".\Content\Textures\plane");

			// Call inherited method.
			base.LoadContent();
		}

		#endregion

		#region IGameComponent Members

		public override void Initialize()
		{
			this.cursor.TextureName = @".\Content\Textures\cursor";

			// Call inherited method.
			base.Initialize();
		}

		#endregion

		#region IUpdateable Members

		public override void Update(GameTime gameTime)
		{
			Vector3 position, vector;
			this.viewpoint.ActiveCamera.Project(this.cursor.Position, out position, out vector);
			CollisionRay cursorRay = new CollisionRay(position, vector);

			CollisionGroup collisionGroup;
			float t;
			if (this.physics.CollisionSystem.IntersectsWith(ref cursorRay, null, out collisionGroup, out position, out vector, out t))
			{
				this.selectedCollisionGroup = collisionGroup;
			}
			else
			{
				this.selectedCollisionGroup = null;
			}

			// Call inherited method.
			base.Update(gameTime);
		}

		#endregion
	}
}


